<template>
  <div class="box">
    <h1>{{title}}</h1>
    <el-form
      :model="ruleForm2"
      status-icon
      :rules="rules2"
      ref="ruleForm2"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="邮箱号" prop="email">
        <el-input v-model="ruleForm2.email" placeholder="请输入您的邮箱"></el-input>
        <el-button @click="sendAuthCode('ruleForm2')" :disabled="flge">
          免费发送验证码
          <span>{{this.time}}</span>
        </el-button>
      </el-form-item>
      <el-form-item label="邮件验证码" prop="emailCode">
        <el-input
          type="text"
          v-model="ruleForm2.emailCode"
          autocomplete="off"
          placeholder="请输入您的邮箱验证码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import $ from "jquery";
import axios from "axios";
import Router from "vue-router";
export default {
  data () {
    var email = (rule, value, callback) => {
      var value = value.toString().trim();
      if (
        !/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(value)
      ) {
        return callback(new Error("邮箱有误格式"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("验证码不能为空"));
      } else if (value.length < 6) {
        callback(new Error("验证码为6位数"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback();
      }
    };
    return {
      title: "邮箱注册",
      flge: false,
      authCode: "",
      time: "",
      email: "",
      ruleForm2: {
        email: ""
      },
      tipsmsg: "",
      rules2: {
        email: [{ validator: email, trigger: "blur" }]
      }
    };
  },
  methods: {
    success () {
      this.$message({
        message: this.tipsmsg,
        type: "success"
      });
    },
    fault () {
      this.$message.error(this.tipsmsg);
    },
    warning () {
      this.$notify({
        title: "警告",
        message: this.tipsmsg,
        type: "warning"
      });
    },
    sendAuthCode (formName) {
      var that = this;
      var email = String(this.ruleForm2.email);
      if (
        /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(email)
      ) {
        var setI = null;
        this.time = 60;
        // 禁用按钮
        this.flge = true;
        // 倒计时取消禁用按钮
        setI = setInterval(() => {
          that.time = --that.time;
          if (this.time == 0) {
            clearInterval(setI);
            this.time = "";
            this.flge = false;
          }
        }, 1000);
        axios.post('api/email', {
          email: email
        })
          .then(res => {
            console.log(res)
            if (res.data.status == 1) {
              this.authCode = res.data.code;
              this.email = res.data.email;
              setTimeout(() => {
                this.authCode = "";
                this.tipsmsg = "刚才发的验证码过期啦，请重新获取验证码";
                this.warning();
              }, 60000);
            }
          }).catch(error => {
            console.log(error)
          })
      }
    },
    submitForm () {
      if (
        this.ruleForm2.emailCode == this.authCode &&
        this.ruleForm2.emailCode != "" &&
        this.email == this.ruleForm2.email
      ) {
        this.tipsmsg = "注册成功";
        this.success();
      } else {
        this.tipsmsg = "验证码错误";
        this.fault();
        return false;
      }
    }
  }
};
</script>
<style lang="stylus" scoped>
.box {
  min-width: 320px;
  max-width: 440px;
  height: 290px;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  background: #f0f0f0;
  border-radius: 10px;
  padding-right: 40px;
}

.cookies_box {
  display: block;
}

h1 {
  font-size: 20px;
  color: #333;
  text-align: center;
  padding: 20px;
  margin-left: 40px;
}

from {
  width: 500px;
}

.register {
  display: block;
  background: #66b1ff;
  border-color: #66b1ff;
  color: #fff;
}
</style>
